package com.newhis.common;

import com.newhis.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 * 该SQL处理类JdbcTemplate直接执行SQL，不需要诸如慢处理等额外过程
 * 下述表名和表结构为示例，实际环境可作调整
 */
@Repository
public class DBSqlHelper {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 记录慢查询
     * @param monStr
     * @param serviceUrl
     * @param duration
     * @return
     */
    public boolean recSqlMon(String monStr, String serviceUrl, long duration) {
        int i = jdbcTemplate.update("insert into comm_slow_sql_rec(mon_str, service, time, duration_ms) values(?, ?, SYSDATE(), ?) ",
                new Object[]{monStr, serviceUrl, duration});
        if (i == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 记录异常日志
     * @param ex
     * @return
     */
    public boolean recExceptionLog(BizException ex) {
        int i = jdbcTemplate.update("insert into comm_log_biz_exception(time, exception_type, info) values(SYSDATE(), ?, ?) ",
                new Object[]{ex.getErrorCode(), ex.getErrorMsg()});
        if (i == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 记录监视日志
     * @param header
     * @param content
     * @return
     */
    public boolean recMonitorLog(String header, String content) {
        int i = jdbcTemplate.update("insert into comm_log_monitor(log_mon_header, time, content) values(?, SYSDATE(), ?) ",
                new Object[]{header, content});
        if (i == 1) {
            return true;
        } else {
            return false;
        }
    }

}
